<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link
      rel="stylesheet"
      href="https://cdn.bootcdn.net/ajax/libs/semantic-ui/2.4.1/semantic.min.css"
    />
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/antd/4.9.2/antd.min.css"
    />
    <style>
      .grandson {
        padding: 8px;
        background-color: aqua;
      }
      .child {
        padding: 8px;
        background-color: rgb(236, 255, 255);
      }
      .father {
        padding: 8px;
        background-color: rgb(5, 61, 61);
      }
      .greatgrandson {
        padding: 8px;
        background-color: rgb(44, 56, 56);
      }
      span {
        color: red;
        font-weight: bold;
      }
    </style>
  </head>
  <body>
    <div id="root"></div>
    <script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.1/umd/react.development.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.1/umd/react-dom.development.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.7.2/prop-types.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/Mock.js/1.0.0/mock-min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/react-router-dom/5.2.0/react-router-dom.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/redux/4.0.5/redux.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/react-redux/7.2.2/react-redux.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/redux-thunk/2.3.0/redux-thunk.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/react-transition-group/4.4.1/react-transition-group.min.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
    <script type="text/babel">
      const { Component, createContext } = React;

      // 你这个上下文状态的名字要是什么名字?
      const UserContext = createContext();
      const { Provider, Consumer } = UserContext;
      // 被包裹的组件,及其后代组件只要申明的类型相同,就可以拿到值
      class A extends Component {
        state = {
          username: "wxs",
          age: 18,
        };
        render() {
          const { username, age } = this.state;
          return (
            <div className="father">
              <h3>我是A组件</h3>
              <h4>我的用户名是:{username}</h4>
              <Provider value={{ username, age }}>
                <B username={username} age={age} />
              </Provider>
            </div>
          );
        }
      }
      class B extends Component {
        render() {
          const { username, age } = this.props;
          return (
            <div className="child">
              <span>props</span>
              <h3>我是B组件</h3>
              <h4>
                我从A组件接收到的用户名是:{username}-{age}
              </h4>
              <C />
            </div>
          );
        }
      }
      class C extends Component {
        // 申明,什么类型是刚刚创建的类型
        static contextType = UserContext;
        render() {
          const { username, age } = this.context;
          return (
            <div className="grandson">
              <span>context</span>
              <h3>我是C组件</h3>
              <h4>
                我从A组件接收到的用户名是:{username}-{age}
              </h4>
              <D />
            </div>
          );
        }
      }
      function D() {
        return (
          <div className="greatgrandson">
            <span>Consumer</span>
            <h3>我是D组件</h3>
            <h4>
              我从A组件接收到的用户名是:
              <Consumer>{(value) => `${value.username},${value.age}`}</Consumer>
            </h4>
          </div>
        );
      }
      ReactDOM.render(<A />, document.getElementById("root"));
    </script>
  </body>
</html>
